package wili.b1;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;

import wili.attrMapping._user.b1.AttConst;
import wili.attrMapping.file.JsonFile;
import wili.c.Ca;
import wili.c.File9a;
import wili.json.JsonArr;
import wili.json.JsonNode;
import wili.json.JsonObj;

/**
 * @author weilai2@163.com
 */
public class FindFromJsonFile9_forB1 implements AttConst {
	static File DIR, FILE;

	static void init() {
		DIR = new File("D:\\bearJ\\awilly\\src\\main\\java\\wili\\b1");
		FILE = new File(DIR, "SalesDoc.detailView_acc.json");
		int opt = 1;
		if (opt == 1) {
		} else if (opt == 2) {
		}
	}

	static void write_all_subSections_groups_items_into_files() {// 取json文件中的 items数组，对其每个元素生成新旧2个文件，便于迁移时比对。
		String st;
		File fromFile, propFile, toRoot;
		{
			fromFile = new File(
					"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\node\\client\\src\\content\\sales\\boODOC\\f\\acc_819c2c615abr\\acc_819c2c615abr.json");
			propFile = new File(
					"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\java\\tcli\\tcli-resources\\target\\classes\\detailView\\SalesDoc.detailView.properties");
			toRoot = new File(
					"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\node\\client\\src\\content\\sales\\boODOC\\fragment\\subSections");
		}
		Map<String, String> pMap = File9a.mapOfFile(propFile, "=");
		JsonFile json = new JsonFile(fromFile);
		List<JsonArr> subs = json.findNodesByPath("subSections");
		List<JsonObj> tabs = json.findObjs_byAtt("subSections", null);
		for (int k = 0; k < subs.size(); k++) {
			JsonArr sub = subs.get(k);
			String subGuid = sub.dad.toObj().guid;
			String subName = pMap.get(subGuid);
			if (subName == null) {
				subName = pMap.get(subGuid + "/label");
				if (subName == null)
					subName = subGuid;
			}
			if (subName.equals("Accounting"))
				Ca.pause();// c705786f-8d01-414a-aefc-819c2c615aar,c705786f-8d01-414a-aefc-819c2c615abr
			File subFile = new File(toRoot, subName + k);
			// String subName = sub.attName;
			List<JsonArr> groupsList = sub.findNodesByPath("groups");
			for (int g = 0; g < groupsList.size(); g++) {
				JsonArr groups = (JsonArr) groupsList.get(g);
				for (JsonNode group : groups.sons()) {
					JsonObj groupObj = group.toObj();
					File groupFile = new File(subFile, groupObj.guid);
					JsonArr items = groupObj.son("items");
					if (items == null) {
						Ca.log(1, " items not found on group: " + groupObj.guid);
						continue;
					}
					List<JsonNode> itemList = items.sons();
					for (int i = 0; i < itemList.size(); i++) {
						JsonNode item = itemList.get(i);
						st = item.toString();
						if (st.contains("6c4c409c-7a09-455c-ba1d-c0dac6b16bc3"))
							item.toString();
						File oldFile = new File(groupFile, "item" + i + ".old.json");// old
						// file.
						File newFile = new File(groupFile, "item" + i + ".json");// new file.
						if ("".isEmpty()) {
							st = item.getString(false);
							File9a.str2file(st, oldFile, -1);
							if (newFile.exists()) {
								Ca.log(1, " newFile:" + newFile.getName() + " already exists.");
							} else {
								File9a.str2file(st, newFile, -1);
								Ca.log(1, " write file: " + groupObj.guid + "/" + oldFile.getName());
							}
						}
					}
				}
			}
		}
	}

	static void findTabsArray_then_find_i18n_forEveryTab() {
		File jfile = new File(
				"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\node\\client\\src\\content\\sales\\boODOC\\fragment\\t\\SalesDoc.detailView_acc.json");
		jfile = new File(
				"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\java\\tcli\\tcli-resources\\target\\classes\\detailView\\SalesDoc.detailView.json");
		File pfile = new File(
				"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\node\\client\\src\\content\\sales\\boODOC\\fragment\\t\\SalesDoc.detailView.properties");
		JsonFile json = new JsonFile(jfile);
		// "label": "{i18n#accounting-tab-payment-block}",
		Predicate<JsonNode> pred = new Predicate<JsonNode>() {
			@Override
			public boolean test(JsonNode t) {
				if (t.attName.equals("tabs"))
					return true;
				return false;
			}
		};
		List<JsonNode> atts = json.findNodes(pred);
		List<JsonNode> lis = atts.get(0).sons();
		Map<String, String> map = File9a.mapOfFile(pfile, "=");
		for (int i = 0; i < lis.size(); i++) {
			JsonNode nod = lis.get(i);
			Ca.log("\n\n===========" + nod.toString());
			List<String> list = json.findAttStringValues__("label", null, new Function<JsonNode, String>() {
				@Override
				public String apply(JsonNode att) {
					Object va = att.stringValue();
					if (va == null)
						att.stringValue();
					String st = va.toString();
					int ind = st.indexOf("#");
					if (ind != -1)
						st = st.substring(ind + 1);
					if (st.endsWith("}")) {
						st = st.substring(0, st.length() - 1);
					}
					return st;
				}
			});
			Set<String> set = new HashSet<>();
			for (String st : list) {
				if (st == null || st.isEmpty())
					continue;
				if (!set.add(st))
					continue;
				Ca.log("# XFLD, 64");
				String va = map.get(st);
				if (va == null)
					va = st;
				Ca.log(st + " = " + va);
			}
		}
	}

	public static void main(String[] args) {
		int opt = 1;
		opt = 2;
		opt = 4;
		if (opt == 1) {
			JsonFile.replaceGuid_demo();
		} else if (opt == 2) {
			findTabsArray_then_find_i18n_forEveryTab();
		} else if (opt == 3) {
			write_all_subSections_groups_items_into_files();
		} else if (opt == 4) {
		}
		Ca.log(1, "end.");
	}
}
